﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using System.Collections.Specialized;
using DevExpress.Skins;
using DevExpress.UserSkins;
using DevExpress.XtraEditors;

namespace ProgNetComponentsDx.Forms
{
    public partial class FormSkinChooser : XtraForm
    {
        public static void RegisterSkins()
        {
            //OfficeSkins.Register();
            BonusSkins.Register();
            
        }

        public static DialogResult ShowSkinChooserDialog()
        {
            return Instance.ShowDialog();
        }
        
        private static FormSkinChooser _Instance;
        private static FormSkinChooser Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new FormSkinChooser();
                    _Instance.Init();
                }
                return _Instance;
            }
        }
        public static void ApplayStyle(string styleName, string skinName)
        {
           
            LookAndFeelStyle style = GetStyle(styleName);
            switch (style)
            {
                case LookAndFeelStyle.Flat:
                    f.LookAndFeel.SetFlatStyle();
                    break;
                case LookAndFeelStyle.UltraFlat:
                    f.LookAndFeel.SetUltraFlatStyle();
                    break;
                case LookAndFeelStyle.Style3D:
                    f.LookAndFeel.SetStyle3D();
                    break;
                case LookAndFeelStyle.Office2003:
                    f.LookAndFeel.SetOffice2003Style();
                    break;
                case LookAndFeelStyle.Skin:
                    f.LookAndFeel.SetSkinStyle(skinName);
                    break;
            }
        }

        private FormSkinChooser()
        {
            InitializeComponent();
        }

        private void Init()
        {
            InitStyles();
            cbeSkin.Properties.Items.Clear();
            cbeSkin.Properties.Items.AddRange(GetSkins());
        }

        private void InitStyles()
        {
            this.cbeStyl.Properties.Items.Clear();
            this.cbeStyl.Properties.Items.Add(StyleNameSkin);
            if (Allow3DStyle)
                this.cbeStyl.Properties.Items.Add(StyleName3D);
            if (AllowFlatStyle)
                this.cbeStyl.Properties.Items.Add(StyleNameFlat);
            if (AllowUltraFlatStyle)
                this.cbeStyl.Properties.Items.Add(StyleNameUltraFlat);
            if (AllowOffice2003Style)
                this.cbeStyl.Properties.Items.Add(StyleNameOffice2003);
        }
       
        #region settings
        private static string _StyleNameFlat = LookAndFeelStyle.Flat.ToString();
        private static string _StyleNameUltraFlat = LookAndFeelStyle.UltraFlat.ToString();
        private static string _StyleName3D = LookAndFeelStyle.Style3D.ToString();
        private static string _StyleNameOffice2003 = LookAndFeelStyle.Office2003.ToString();
        private static string _StyleNameSkin = LookAndFeelStyle.Skin.ToString();
        private static bool _AllowFlatStyle = true;
        private static bool _AllowUltraFlatStyle = true;
        private static bool _Allow3DStyle = true;
        private static bool _AllowOffice2003Style = true;

        [Category(ProgNetComponents.Constants.Categories.ProgNetAppearance)]
        public static bool AllowFlatStyle
        {
            get
            {
                return _AllowFlatStyle;
            }
            set
            {
                _AllowFlatStyle = value;
            }
        }
        [Category(ProgNetComponents.Constants.Categories.ProgNetAppearance)]
        public static bool AllowUltraFlatStyle
        {
            get
            {
                return _AllowUltraFlatStyle;
            }
            set
            {
                _AllowUltraFlatStyle = value;
            }
        }
        [Category(ProgNetComponents.Constants.Categories.ProgNetAppearance)]
        public static bool Allow3DStyle
        {
            get
            {
                return _Allow3DStyle;
            }
            set
            {
                _Allow3DStyle = value;
            }
        }
        [Category(ProgNetComponents.Constants.Categories.ProgNetAppearance)]
        public static bool AllowOffice2003Style
        {
            get
            {
                return _AllowOffice2003Style;
            }
            set
            {
                _AllowOffice2003Style = value;
            }
        }

        [Category(ProgNetComponents.Constants.Categories.ProgNetAppearance)]
        public static string StyleNameFlat
        {
            get
            {
                if (_StyleNameFlat.IsNullOrEmpty())
                    return LookAndFeelStyle.Flat.ToString();
                else
                    return _StyleNameFlat;
            }
            set { _StyleNameFlat = value; }
        }
        [Category(ProgNetComponents.Constants.Categories.ProgNetAppearance)]
        public static string StyleNameUltraFlat
        {
            get
            {
                if (_StyleNameUltraFlat.IsNullOrEmpty())
                    return LookAndFeelStyle.UltraFlat.ToString();
                else
                    return _StyleNameUltraFlat;
            }
            set { _StyleNameUltraFlat = value;  }
        }
        [Category(ProgNetComponents.Constants.Categories.ProgNetAppearance)]
        public static string StyleName3D
        {
            get
            {
                if (_StyleName3D.IsNullOrEmpty())
                    return LookAndFeelStyle.Style3D.ToString();
                else
                    return _StyleName3D;
            }
            set { _StyleName3D = value; }
        }
        [Category(ProgNetComponents.Constants.Categories.ProgNetAppearance)]
        public static string StyleNameOffice2003
        {
            get
            {
                if (_StyleNameOffice2003.IsNullOrEmpty())
                    return LookAndFeelStyle.Office2003.ToString();
                else
                    return _StyleNameOffice2003;
            }
            set { _StyleNameOffice2003 = value;  }
        }
        [Category(ProgNetComponents.Constants.Categories.ProgNetAppearance)]
        public static string StyleNameSkin
        {
            get
            {
                if (_StyleNameSkin.IsNullOrEmpty())
                    return LookAndFeelStyle.Skin.ToString();
                else
                    return _StyleNameSkin;
            }
            set { _StyleNameSkin = value;  }
        }

        #endregion

        public UserLookAndFeel GetNewLookAndFeel()
        {
            UserLookAndFeel look = new UserLookAndFeel(this);
            look.SetSkinStyle(skinName);
            return look;
        }

        private string skinName;
        LookAndFeelStyle oldStyle;
        bool oldUseXpTheme;

        private string oldSkin;
        static DefaultLookAndFeel f = new DefaultLookAndFeel();

        protected virtual string[] GetSkins()
        {
            List<string> skins = new List<string>();
            foreach (SkinContainer skin in SkinManager.Default.Skins)
                skins.Add(skin.SkinName);
            
            return skins.ToArray();
        }

        private void FormColorShema_Load(object sender, EventArgs e)
        {
            Init();
            cbeSkin.SelectedItem = LookAndFeel.ActiveSkinName;
            cbeStyl.SelectedItem = GetStyleName(LookAndFeel.Style);

            oldStyle = LookAndFeel.Style;
            oldUseXpTheme = LookAndFeel.UseWindowsXPTheme;

            oldSkin = LookAndFeel.ActiveSkinName;
        }

        private void cbeSkin_SelectedIndexChanged(object sender, EventArgs e)
        {
            skinName = cbeSkin.SelectedItem.ToString();
            f.LookAndFeel.Assign(GetNewLookAndFeel());
            this.Refresh();
        }

        private void FormColorShema_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (DialogResult != DialogResult.OK)
            {
                if (f.LookAndFeel.UseWindowsXPTheme != oldUseXpTheme)
                    f.LookAndFeel.UseWindowsXPTheme = oldUseXpTheme;
                if (f.LookAndFeel.UseWindowsXPTheme)
                    f.LookAndFeel.SetWindowsXPStyle();
                if (f.LookAndFeel.Style != oldStyle || oldStyle == LookAndFeelStyle.Skin)
                {
                    f.LookAndFeel.Style = oldStyle;
                    switch (oldStyle)
                    {
                        case LookAndFeelStyle.Flat:
                            f.LookAndFeel.SetFlatStyle();
                            break;
                        case LookAndFeelStyle.UltraFlat:
                            f.LookAndFeel.SetUltraFlatStyle();
                            break;
                        case LookAndFeelStyle.Style3D:
                            f.LookAndFeel.SetStyle3D();
                            break;
                        case LookAndFeelStyle.Office2003:
                            f.LookAndFeel.SetOffice2003Style();
                            break;
                        case LookAndFeelStyle.Skin:
                            f.LookAndFeel.SetSkinStyle(oldSkin);
                            break;
                    }
                }
            }
            else
            {

            }
        }

        private void pnxButtonPanel1_OKClick(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void cbeStyl_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbeSkin.Enabled = cbeStyl.SelectedItem.AsString() == StyleNameSkin;
            LookAndFeelStyle style = GetStyle(cbeStyl.SelectedItem.AsString());
            switch (style)
            {
                case LookAndFeelStyle.Flat:
                    f.LookAndFeel.SetFlatStyle();
                    break;
                case LookAndFeelStyle.UltraFlat:
                    f.LookAndFeel.SetUltraFlatStyle();
                    break;
                case LookAndFeelStyle.Style3D:
                    f.LookAndFeel.SetStyle3D();
                    break;
                case LookAndFeelStyle.Office2003:
                    f.LookAndFeel.SetOffice2003Style();
                    break;
                case LookAndFeelStyle.Skin:
                    f.LookAndFeel.SetSkinStyle(cbeSkin.SelectedItem.AsString());
                    break;
            }
            this.Refresh();
        }

        private static LookAndFeelStyle GetStyle(string name)
        {
            if (name == StyleNameSkin)
                return LookAndFeelStyle.Skin;
            if (name == StyleNameFlat)
                return LookAndFeelStyle.Flat;
            if (name == StyleNameUltraFlat)
                return LookAndFeelStyle.Flat;
            if (name == StyleNameOffice2003)
                return LookAndFeelStyle.Office2003;
            if (name == StyleName3D)
                return LookAndFeelStyle.Style3D;

            return LookAndFeelStyle.Skin;
        }

        private string GetStyleName(LookAndFeelStyle style)
        {
            switch (style)
            {
                case LookAndFeelStyle.Skin:
                    return StyleNameSkin;
                case LookAndFeelStyle.Flat:
                    return StyleNameFlat;
                case LookAndFeelStyle.UltraFlat:
                    return StyleNameUltraFlat;
                case LookAndFeelStyle.Office2003:
                    return StyleNameOffice2003;
                case LookAndFeelStyle.Style3D:
                    return StyleName3D;
                default:
                    return StyleNameSkin;
            }
        }

    }

}
